package com.aidex.biz.service.impl;

import com.aidex.biz.domain.BillInfoDetails;
import com.aidex.biz.domain.Order;
import com.aidex.biz.mapper.BillInfoDetailsMapper;
import com.aidex.biz.service.BillInfoDetailsService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.config.AiDexConfig;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.utils.NumberUtils;
import com.aidex.system.service.SysDictDataService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 对账详情Service业务层处理
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-09-27
 */
@Service
@Transactional(readOnly = true)
public class BillInfoDetailsServiceImpl extends BaseServiceImpl<BillInfoDetailsMapper, BillInfoDetails> implements BillInfoDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(BillInfoDetailsServiceImpl.class);

    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * 获取单条数据
     * @param billInfoDetails 对账详情
     * @return 对账详情
     */
    @Override
    public BillInfoDetails get(BillInfoDetails billInfoDetails)
    {
        BillInfoDetails dto = super.get(billInfoDetails);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 对账详情id
     * @return 对账详情
     */
    @Override
    public BillInfoDetails get(String id)
    {
        BillInfoDetails dto = super.get(id);
        return dto;
    }


    /**
     * 查询对账详情列表
     * @param billInfoDetails 对账详情
     * @return 对账详情
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public List<BillInfoDetails> findList(BillInfoDetails billInfoDetails)
    {
        return super.findList(billInfoDetails);
    }

    /**
     * 分页查询对账详情列表
     * @param billInfoDetails 对账详情
     * @return 对账详情
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public PageInfo<BillInfoDetails> findPage(BillInfoDetails billInfoDetails)
    {
        return super.findPage(billInfoDetails);
    }

    /**
     * 保存对账详情
     * @param billInfoDetails
     * @return 结果
     */
    @Override
    public boolean save(BillInfoDetails billInfoDetails)
    {
        return super.save(billInfoDetails);
    }


    /**
     * 删除对账详情信息
     * @param billInfoDetails
     * @return 结果
     */
    @Override
    public boolean remove(BillInfoDetails billInfoDetails)
    {
        return super.remove(billInfoDetails);
    }

    /**
     * 批量删除对账详情
     * @param ids 需要删除的对账详情ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBillInfoDetailsByIds(String[] ids)
    {
        return mapper.deleteBillInfoDetailsByIds(ids);
    }

    /**
     * 获取最大编号
     * @param billInfoDetails 对账详情
     * @return 结果
     */
    @Override
    public int findMaxSort(BillInfoDetails billInfoDetails)
    {
        return NumberUtils.nextOrder(mapper.findMaxSort(billInfoDetails));
    }

    /**
     * 订单完成后推送订单到账单中心 计算佣金
     * @param order
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public Boolean orderToBillInfoDetails(Order order) {
        BillInfoDetails details=new BillInfoDetails();
        if(!AiDexConfig.getCompanyId().equals(order.getCompanyId())){
            if("1".equals(order.getOrderType())){
                // 商城订单结算
                details.setMoney(order.getPayAmount().multiply(new BigDecimal(sysDictDataService.selectDictValue("ticheng_li","mall"))));
            }else {
                //洗护订单佣金结算比例
                details.setMoney(order.getPayAmount().multiply(new BigDecimal(sysDictDataService.selectDictValue("ticheng_li","xihu"))));
            }
            details.setOrderId(order.getId());
            details.setCompanyId(order.getCompanyId());
            details.setType(order.getOrderType()); // 订单类型
            details.setStatus("0");
            return this.save(details);
        }
        return false;
    }



    /** 定时任务
     * 统计每个公司的日账单
     * @param billInfoDetails
     * @return
     */
    @Override
    public BillInfoDetails sumToDayBillDetails(BillInfoDetails billInfoDetails) {
        return mapper.sumToDayBillDetails(billInfoDetails);
    }

    /**
     * 修改已经统计的账单
     * @param billInfoDetails
     * @return
     */
    @Override
    public Integer updateSumToDayBillDetails(BillInfoDetails billInfoDetails) {
        return mapper.updateSumToDayBillDetails(billInfoDetails);
    }

    @Override
    public BillInfoDetails findDay(BillInfoDetails billInfoDetails) {
        return mapper.findDay(billInfoDetails);
    }


}
